// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CImplRange.
//   $Id$



#include "stdafx.h"
#include "range.h"
#include "vertex.h"
#include "namespaces.h"

const SQName CImplRange::sm_arrQNAvailableTypes[RANGE_TYPE_COUNT]
=
{
//	Derived built-in types:  If we get a better 
//	schema processor, we might be able to drop these.
	{ws_NSURI_XSD,L"integer"},
	{ws_NSURI_XSD,L"nonPositiveInteger"},
	{ws_NSURI_XSD,L"negativeInteger"},
	{ws_NSURI_XSD,L"long"},
	{ws_NSURI_XSD,L"int"},
	{ws_NSURI_XSD,L"short"},
	{ws_NSURI_XSD,L"byte"},
	{ws_NSURI_XSD,L"nonNegativeInteger"},
	{ws_NSURI_XSD,L"unsignedLong"},
	{ws_NSURI_XSD,L"unsignedInt"},
	{ws_NSURI_XSD,L"unsignedShort"},
	{ws_NSURI_XSD,L"unsignedByte"},
	{ws_NSURI_XSD,L"positiveInteger"},
//Basic Built in types
	{ws_NSURI_XSD,L"duration"},
	{ws_NSURI_XSD,L"date"},
	{ws_NSURI_XSD,L"time"},
	{ws_NSURI_XSD,L"dateTime"},
	{ws_NSURI_XSD,L"gYearMonth"},
	{ws_NSURI_XSD,L"gYear"},
	{ws_NSURI_XSD,L"gMonthDay"},
	{ws_NSURI_XSD,L"gDay"},
	{ws_NSURI_XSD,L"gMonth"},
	{ws_NSURI_XSD,L"decimal"},
	{ws_NSURI_XSD,L"float"},
	{ws_NSURI_XSD,L"double"}
};

// range needs to check the types to which it is bound
void CImplRange::buildDependencies(void)
{
	__super::buildDependencies();
	int i  = m_vertices.GetSize();
	//A range is bound to a node of the correct type iff:-
	//	the node is of the correct type
	//	the node does not exist.
	bool bIsCorrectType = true;
	//TODO: reenable following code
	//	binding to integer seemed not to work?
	//	this is due to derivation.
	if(i > 0)
	{	
		CBoundNode * pNode = m_vertices[0]->m_pInstanceNode;
		if(pNode)
		{
			bIsCorrectType = pNode->isOneOfType(sm_arrQNAvailableTypes,RANGE_TYPE_COUNT);
		}
	}


	if(!bIsCorrectType)
	{
		m_vertices[0]->m_pInstanceNode->RemoveTarget(this);
		m_vertices.RemoveAt(0);
		onBindToNothing();
	}
}

// Variables m_lEnd & m_lStart changed from m_ulEnd & m_ulStart
// Fixes Bug 277
CImplRange::CImplRange(CXFormsElementRange* pElement)
	: CImplSingleNodeBinding((CXFormsElement*) pElement)
	, m_dStart(0)
	, m_dEnd(0)
	, m_dStep(0)
{
	m_dStart = new double;
	*m_dStart = 0;
	m_dEnd = new double;
	*m_dEnd = 10;
	m_dStep = new double;
	*m_dStep = 1;

	CFormControl::SetControlType(L"<range>");
}

CImplRange::~CImplRange()
{
	delete m_dStart;
	delete m_dEnd;
	delete m_dStep;
	m_dStart = m_dEnd = m_dStep = 0;
}
